const express = require('express');
const User = require('./model/user.js');
const Topic = require('./model/topic.js');
const md5 = require('blueimp-md5');

const router = express();

router.get('/',(req,res) => {
	Topic.find((err,data) => {
		if(err) {
			return res.end('Server Error');
		}
		let PageCode = [];
		let length = Math.ceil(data.length / 6);
		for(var i = 1;i <= length;i++) {
			PageCode.push(i);
		}
		data = data.slice(0,6);
		res.render('index.html',{user:req.session.user,topic:data,pageCode:PageCode});
	})
})



router.get('/page/choose',(req,res) => {
	const num = parseInt(req.query.num,10);
	
	const startNum = (num - 1) * 6;
	const endNum = startNum + 6;
	Topic.find((err,data) => {
		if(err) {
			return res.end('Server Error');
		}
		data = data.slice(startNum,endNum);
		res.json({topic:data});
	})
})



router.get('/register',(req,res) => {
	res.render('register.html');
})

router.post('/register',(req,res) => {
	let body = req.body;
	User.findOne({
		$or:[
			{
				email:body.email
			},
			{
				nickname:body.nickname
			}
		]
	},(err,data) => {
		if(err) {
			return res.status(500).json({
				err_code:500,
				message:"server Error 服务端错误"
			})
			// return next(err)
		}
		if(data) {
			// 邮箱或者昵称已存在
			return res.status(200).json({
				err_code:1,
				message: 'Email or nickname aleady exists.'
			})
		}

		// 对密码进行 md5 重复加密
		body.password = md5(md5(body.password));
		new User(body).save((err,data) => {
			if(err) {
				return res.status(500).json({
					err_code:500,
				})
			}
			req.session.user = data;
			return res.status(200).json({
				err_code:0,
			})
		})
	})
})

router.get('/login',(req,res) => {
	res.render('login.html');
})

router.post('/login',(req,res) => {
	let body = req.body;
	body.password = md5(md5(body.password));
	User.findOne({
		email:body.email,
		password:body.password
	},(err,data) => {
		if(err) {
			return res.status(500).json({
				err_code:500,
			})
		}
		if(!data) {
			//不成功就提示
			return res.status(200).json({
				err_code:1,
			})
		}
		// 注册成功的话：
		// 注册成功就会用 session 记录登陆状态
		req.session.user = data;

		res.status(200).json({
			err_code:0,
			message: 'OK'
		})
	})
})



router.get('/topics/new',(req,res) => {
	// session保存变量user
	res.render('topic/new.html',{user:req.session.user});
})



router.post('/topics/new',(req,res) => {
	var body = req.body;
	new Topic(body).save((err,data) => {
		if(err) {
			return res.end('Server Error');
		}
		res.redirect('/');
	})
})



router.get('/topics/show',(req,res) => {
	Topic.findOne({_id:req.query.id},(err,data) => {
		if(err) {
			throw(err);
		}
		res.render('topic/show.html',{topic:data,user:req.session.user})
	})
})


//渲染页面，附带session保存到的user，保证为一登陆状态
router.get('/settings/profile',function(req,res) {
	res.render('settings/profile.html',{user:req.session.user});
})

router.post('/settings/profile',function(req,res) {
	var body = req.body;
	body.birthday = body.birthday + '';
	User.findByIdAndUpdate(req.session.user._id,body,function(err,data) {
		if(err) {//有误时
			return res.json({
				err_code:500
			})
		}
		req.session.user = data;
		// console.log(req.session.user);//输出要修改的信息
		res.json({
			err_code:0,
			
		})
	})
})


//退出账号
router.get('/logout',(req,res) => {
	req.session.user = null;
	res.redirect('/');
})



router.post('/comment_save',(req,res) => {
	var body = req.body;
	new Topic(body).save((err,data) => {
		if(err) {
			return res.end('Server Error');
		}
		res.redirect('/');
	})
})







module.exports = router;